import axios from 'axios'
import Config from '@/http/config.js'
import { Message } from 'element-ui'

// create an axios instance
const service = axios.create({
  baseURL: Config.apiUrl,
  headers: {
    Accept: '*/*'
  },
  timeout: Config.timeout,
  withCredentials: true
})
// request interceptor
service.interceptors.request.use(
  config => {
    return config
  },
  error => {
    Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    if (response.status === 200) {
      return Promise.resolve(response.data)
    } else {
      return Promise.reject(response)
    }
  },
  error => {
    Message({
      message: '系统开小差，请稍后再试！',
      type: 'error'
    })
    return Promise.reject(error)
  }
)

export default service
